package 力扣.二叉树相关问题.中度题;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class 特定深度节点链表 {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

  public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
  }
    public ListNode[] listOfDepth(TreeNode tree) {
        if (tree==null)
            return null;
        List<ListNode> lists = new ArrayList<>();
        Queue<TreeNode> q=new LinkedList();
        q.offer(tree);
        while (!q.isEmpty()){
            ListNode node = new ListNode(-1);
            ListNode pre=node;
            for (int i = q.size(); i>0 ; i--) {
                TreeNode poll = q.poll();
                pre.next=new ListNode(poll.val);
                pre=pre.next;
                if(poll.left!=null)
                    q.offer(poll.left);
                if (poll.right!=null)
                    q.offer(poll.right);
            }
            lists.add(node.next);
        }
        ListNode[] res=lists.toArray(new ListNode[lists.size()]);
        return res;
    }
}
